package com.example.service.Impl;

import com.example.entity.LoginEntity;
import com.example.entity.LoginResponse;
import com.example.mapper.UserMapper;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.utils.BCryptPasswordUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.utils.RedisCache;

import java.time.LocalDateTime;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void register(User user) {
        log.info("对密码进行进行哈希处理");
        String encode = BCryptPasswordUtils.encodePassword(user.getPassword());
        user.setPassword(encode);
        user.setRegisterTime(LocalDateTime.now());
        userMapper.register(user);
    }

    @Override
    public void logout(String token, Integer id) {
        redisCache.deleteObject(token);
        LocalDateTime localDateTime = LocalDateTime.now();
        userMapper.setStatusOut(id,localDateTime);
    }

    @Override
    public boolean usernameExist(String username) {
        return userMapper.usernameExist(username);
    }

    @Override
    public Integer getIdByUsername(String username) {
        return userMapper.getIdByUsername(username);
    }

    @Override
    public String identifyPassword(Integer id, String oldPwd) {
        String password = userMapper.queryPasswordById(id);
        if (!BCryptPasswordUtils.matchPassword(oldPwd, password)) {
            log.info("原密码填写错误");
            return "原密码填写错误";
        }
        return null;
    }

    @Override
    public void updatePassword(Integer id, String newPwd) {
        newPwd = BCryptPasswordUtils.encodePassword(newPwd);
        userMapper.updatePassword(id, newPwd);
    }

    @Override
    public LoginResponse findUserInfo(Integer id) {
        return userMapper.findUserInfo(id);
    }

    @Override
    public void updateUserInfo(User user) {
        userMapper.updateUserInfo(user);
    }

    @Override
    public void updateProfile(String profile, Integer id) {
        userMapper.updateProfile(profile,id);
    }

    @Override
    public Integer login(LoginEntity user) {
        String password = userMapper.login(user.getUsername(), user.getPermission());
        if (password == null) {
            return 0; //用户未找到
        } else {
            //用户提供的未加密密码
            String userPassword = user.getPassword();
            if (BCryptPasswordUtils.matchPassword(userPassword, password)) {
                LocalDateTime localDateTime = LocalDateTime.now();
                userMapper.setStatus(user.getUsername(),localDateTime);
                return 1; //密码匹配
            } else {
                return 2; //密码不匹配
            }
        }
    }

    @Override
    public Integer emailExist(String email) {
        log.info("对注册用户的邮箱进行判断不能重复");
        return userMapper.emailExist(email);
    }
}
